<?php
/**
 * Created by PhpStorm.
 * User: yaolei
 * Date: 2017/11/16
 * Time: 23:35
 */

namespace Home\Model;
use Think\Model;

class Goods extends Model
{
    protected $tableName = 'balance_goods';
    public function getData(){
        $data=$this->data;
        if($this->type==1){
            if(!empty($data['user_id'])){
                $users= new Model('user_balance');
                $data['user']=$users->where('user_id='.$data['user_id'])->find();
            }
        }
        return $data;
    }

    public function generateSN(){
        $sn="";
        $Model = new Model('balance_goods');
        do {
            $sn= date('Ymd') . str_pad(mt_rand(1, 9999999999),10, '0', STR_PAD_LEFT);
            $count=$Model->where("goods_sn='".$sn."'")->count();
        } while ($count>0);
        $this->goods_sn=$sn;
    }

    public function add(){
        $data=$this->getData();
        $user=$data['user'];

        if(empty(trim($data['name']))){
            $this->error="请填写产品名称";
            return false;
        }
        if(empty(trim($data['price']))){
            $this->error="请输入价格，且必须大于0";
            return false;
        }
        if(!is_numeric(trim($data['price']))){
            $this->error="价格只能为数字，且大于0";
            return false;
        }
        if(trim($data['price'])<=0){
            $this->error="价格只能为数字，且大于0";
            return false;
        }

        $goods = new Model('balance_goods');
        $count=$goods->where("name='".trim($data['name'])."'")->count();
        if($count>0){
            $this->error="产品名称已经存在";
            return false;
        }
        $model = new Model();
        $model->startTrans();
        $flag=false;

        if($this->type==1){
            if(empty($user)){
                $this->error="用户信息有误，请重新选择";
            }
            $number=floor($data['balance']);

            if($number<=0){
                $this->error="转入数必须大于0";
                return false;
            }
            if($number>$user['balance']){
                $this->error="出售的余额不能大于".$user['balance'];
                return false;
            }
        }
        $this->generateSN();
        if($this->type==1){
            $user_balance=$user['balance']-$this->balance;
            $this->username=$user['username'];
            $res1=$model->table(C('DB_PREFIX').'user_balance')->where("id=".$user['id'])->save([
                'balance'=>$user_balance,
                'use_balance'=>$user['use_balance']+$this->balance,
                'frozen_balance'=>$user['frozen_balance']+$this->balance,
                'updatedtime'=>time()
            ]);
            if($res1){
                //用户支出余额
                $model->table(C('DB_PREFIX').'user_balance_log')->add([
                    'user_id'=>$user['user_id'],
                    'type'=>1,
                    'current_balance'=>$user['balance'],
                    'balance'=>$this->balance,
                    'total_balance'=>$user_balance,
                    'createdtime'=>time(),
                    'description'=>'用户：'.$user['username'].' 出售余额，产品号：'.$this->goods_sn
                ]);
                $flag=true;
            }
        }else{
            $flag=true;
        }
        if(!$flag){
            $model->rollback();
        }else{
            $this->createdtime=time();
            parent::add();
            $model->commit();
        }
        return $flag;
    }

    public function update(){
        $data=$this->getData();
        $user=$data['user'];

        if(empty(trim($data['name']))){
            $this->error="请填写产品名称";
            return false;
        }
        if(empty(trim($data['price']))){
            $this->error="请输入价格，且必须大于0";
            return false;
        }
        if(!is_numeric(trim($data['price']))){
            $this->error="价格只能为数字，且大于0";
            return false;
        }
        if(trim($data['price'])<=0){
            $this->error="价格只能为数字，且大于0";
            return false;
        }

        $goods = new Model('balance_goods');
        $count=$goods->where("name='".trim($data['name'])."' AND id<>".$data['id'])->count();
        if($count>0){
            $this->error="产品名称已经存在";
            return false;
        }
        return $this->save();
    }


}